/*
 * Copyright 2010 WithOne Co. Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jp.co.withone.osgi.gadget.fileconnector;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;

/**
 * The FileConnectorSettingView class provide FileConnector setting view.
 * 
 * @author funakky
 */
public class FileConnectorSettingView extends JFrame {

	static String dirPath = "C:\\pic";

	/**
	 * Create new FileConnectorSettingView object so that initilized components.
	 */
	public FileConnectorSettingView() {
		super(Activator.RB.getString("setting.view.title"));
		initComponents();
	}

	private void initComponents() {
		final JFrame frame = this;

		// create components
		final JLabel dirLabel = new JLabel(Activator.RB.getString("dir.path"));
		final JTextField dirText = new JTextField(dirPath);
		dirText.setPreferredSize(new Dimension(300, 20));
		final JButton chooseButton = new JButton(Activator.RB
				.getString("choose"));
		chooseButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				final JFileChooser chooser = new JFileChooser(dirText.getText());
				chooser.setFileHidingEnabled(false);
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int returnVal = chooser.showOpenDialog(chooser);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					java.io.File file = chooser.getSelectedFile();
					dirText.setText(file.getAbsolutePath());
				}
			}
		});
		final JButton okButton = new JButton(Activator.RB.getString("ok"));
		final JButton applyButton = new JButton(Activator.RB.getString("apply"));
		final JButton cancelButton = new JButton(Activator.RB
				.getString("cancel"));

		final ActionListener actionListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				final Object source = e.getSource();
				if (source == okButton || source == applyButton) {
					dirPath = dirText.getText();
				}
				if (source == okButton || source == cancelButton) {
					frame.dispose();
				}
			}
		};

		okButton.addActionListener(actionListener);
		applyButton.addActionListener(actionListener);
		cancelButton.addActionListener(actionListener);

		// set layout
		final GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);

		{
			// set horizontal group
			final ParallelGroup pg1 = layout
					.createParallelGroup(Alignment.TRAILING);
			pg1.addComponent(dirLabel).addComponent(okButton);
			final ParallelGroup pg2 = layout
					.createParallelGroup(Alignment.TRAILING);
			pg2.addComponent(dirText).addComponent(applyButton);
			final ParallelGroup pg3 = layout
					.createParallelGroup(Alignment.TRAILING);
			pg3.addComponent(chooseButton).addComponent(cancelButton);

			layout.setHorizontalGroup(layout.createSequentialGroup().addGroup(
					pg1).addGroup(pg2).addGroup(pg3));
		}
		{
			// set vertical group
			final ParallelGroup pg1 = layout
					.createParallelGroup(Alignment.BASELINE);
			pg1.addComponent(dirLabel).addComponent(dirText).addComponent(
					chooseButton);
			final ParallelGroup pg2 = layout
					.createParallelGroup(Alignment.BASELINE);
			pg2.addComponent(okButton).addComponent(applyButton).addComponent(
					cancelButton);

			layout.setVerticalGroup(layout.createSequentialGroup()
					.addGroup(pg1).addGroup(pg2));
		}

		pack();
	}
}
